package com.fairytask.logic
{
	import com.fairytask.data.Modulator;
	import com.fairytask.event.BattleEvent;
	import com.fairytask.event.TickEvent;
	import com.fairytask.layer.BattleLayer;
	import com.fairytask.objects.Cell;
	import com.fairytask.utils.Tick;
	import com.fairytask.utils.TimeUtil;
	
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;

	/**
	 * 战斗逻辑 
	 * @author game
	 * 
	 */	
	public class BattleLogic extends EventDispatcher
	{
		private static var _instance	:BattleLogic;

		//数据
		private var model			:Modulator;
		private var selectedCells	:Array	=	[];
		//实例化
		private var battleLayer	:BattleLayer;
		private var cStatus:Object;
		private var mouseStat:String;
		private var isTick:Boolean;
		private var timeUtil:TimeUtil;
		
		public static function get instance():BattleLogic
		{
			if(null==_instance)
			{
				_instance	=	new BattleLogic();
			}
			return _instance;
		}

		
		public function BattleLogic()
		{
			super();
			model	=	Modulator.instance;
			battleLayer	=	BattleLayer.instance;
			timeUtil=	new TimeUtil();
			initEvent();
		}
		
		private function initEvent():void
		{
			model.addEventListener(BattleEvent.ENTER_BATTLE,onEnter);
			//添加帧事件
			Tick.instance.addEventListener(TickEvent.TICK,onUpdate);
		}
		
		/**
		 * 每帧都干的事 
		 * @param event
		 * 
		 */
		protected function onUpdate(event:TickEvent):void
		{
			timeUtil.update();
			if(null!=battleLayer&&battleLayer.isStart)
			{
				battleLayer.update(timeUtil.elapsed,timeUtil.dt);
			}
		}
		
		/**
		 * 进入战斗 
		 * @param evt
		 * 
		 */
		private function onEnter(evt:BattleEvent):void
		{
			battleLayer.initById(evt.bData);
			//设置当前玩家和对手
		}
		
		/**
		 * 鼠标移过 
		 * @param event
		 * 
		 */
		protected function onOver(event:MouseEvent):void
		{
			if(!(event.target is Cell))return;
			var tmpCell	:Cell	=	event.target as Cell;
			//判断是不是自己的
		}
		
		protected function onOut(event:MouseEvent):void
		{
			if(!(event.target is Cell))return;
			var tmpCell	:Cell	=	event.target as Cell;
			//看一下是不是选中的
		}
		
		/**
		 * 鼠标事件 
		 * @param evt
		 * 
		 */
		private function onDownExcute(evt:MouseEvent):void
		{
			battleLayer.mouseDown	=	true;
			if(!(evt.target is Cell))return;
		}
		
		protected function onUpExcute(event:MouseEvent):void
		{
			if(!(event.target is Cell))
			{
				//停止画线 停止事件
			}
		}
		
	}
}